package factorybean;

import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.time.LocalDate;

/**
 * @author wx-li
 * @company 希睿科技(www.xraitech.com)
 * @copyright (c) 2018 希睿科技( www.xraitech.com )Inc. All rights reserved.
 * @date 2020/10/16 17:01
 * @since JDK1.8
 */
public class Test {
    public static void main(String[] args) throws Exception {
        ApplicationContext applicationContext =
                new ClassPathXmlApplicationContext("factorybean.xml");
        Object nextDayDateDisplayer = applicationContext.getBean("nextDayDateDisplayer");
        System.out.println(nextDayDateDisplayer);

        Object nextDayDate = applicationContext.getBean("nextDayDate");
        assertTrue(nextDayDate instanceof LocalDate);

        // 使用&获取FactoryBean
        Object factoryBean = applicationContext.getBean("&nextDayDate");
        assertTrue(factoryBean instanceof FactoryBean);
        assertTrue(factoryBean instanceof NextDayDateFactoryBean);

        Object factoryValue = ((FactoryBean) factoryBean).getObject();
        assertTrue(factoryValue instanceof LocalDate);

    }

    private static void assertTrue(boolean result) {
        if (!result) {
            throw new RuntimeException("断言为假");
        }
    }

}
